package com.mqttinsight.ui.form.panel;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.mqttinsight.exception.VerificationException;
import com.mqttinsight.mqtt.SecureSetting;
import com.mqttinsight.util.LangUtil;
import com.mqttinsight.util.Validator;

import javax.swing.*;

/**
 * @author ptma
 */
public class ServerAndClientPanel extends SecurePanel {
    private JPanel rootPanel;
    private JTextField serverCertificateFileField;
    private JPasswordField clientKeyPasswordField;
    private JLabel serverCertificateFileLabel;
    private JLabel clientKeyPasswordLabel;
    private JTextField clientCertificateFileField;
    private JTextField clientKeyFileField;
    private JCheckBox clientKeyPemCheckBox;
    private JLabel clientCertificateFileLabel;
    private JLabel clientKeyFileLabel;

    public ServerAndClientPanel() {
        super();
        $$$setupUI$$$();
        initSecureFileBrowserButton(serverCertificateFileField);
        initSecureFileBrowserButton(clientCertificateFileField);
        initSecureFileBrowserButton(clientKeyFileField);
        applyLanguage();
    }

    @Override
    public void applyLanguage() {
        serverCertificateFileLabel.setText(LangUtil.getString("CaCertificateFile"));
        clientKeyPasswordLabel.setText(LangUtil.getString("ClientKeyPassword"));
        clientCertificateFileLabel.setText(LangUtil.getString("ClientCertificateFile"));
        clientKeyFileLabel.setText(LangUtil.getString("ClientKeyFile"));
        clientKeyPemCheckBox.setText(LangUtil.getString("PemKey"));
    }

    @Override
    public JPanel getRootPanel() {
        return rootPanel;
    }

    @Override
    public void changeFieldsEnable(boolean enabled) {
        serverCertificateFileField.setEnabled(enabled);
        clientCertificateFileField.setEnabled(enabled);
        clientKeyFileField.setEnabled(enabled);
        clientKeyPasswordField.setEnabled(enabled);
        clientKeyPemCheckBox.setEnabled(enabled);
    }

    @Override
    public void applySetting(SecureSetting setting) {
        if (setting != null) {
            serverCertificateFileField.setText(setting.getServerKeyStoreFile());
            clientCertificateFileField.setText(setting.getClientCertificateFile());
            clientKeyFileField.setText(setting.getClientKeyFile());
            clientKeyPasswordField.setText(setting.getClientKeyPassword());
            clientKeyPemCheckBox.setSelected(setting.isClientKeyPEM());
        }
    }

    @Override
    public void resetFields() {
        serverCertificateFileField.setText("");
        clientCertificateFileField.setText("");
        clientKeyFileField.setText("");
        clientKeyPasswordField.setText("");
        clientKeyPemCheckBox.setSelected(false);
    }

    @Override
    public void verifyFields() throws VerificationException {
        Validator.notEmpty(serverCertificateFileField, () -> LangUtil.format("FieldRequiredValidation", serverCertificateFileLabel.getText()));
        Validator.notEmpty(clientCertificateFileField, () -> LangUtil.format("FieldRequiredValidation", clientCertificateFileLabel.getText()));
        Validator.notEmpty(clientKeyFileField, () -> LangUtil.format("FieldRequiredValidation", clientKeyFileLabel.getText()));
        Validator.notEmpty(clientKeyPasswordField, () -> LangUtil.format("FieldRequiredValidation", clientKeyPasswordLabel.getText()));
    }

    @Override
    public SecureSetting getSetting() throws VerificationException {
        SecureSetting setting = new SecureSetting();
        setting.setServerKeyStoreFile(serverCertificateFileField.getText());
        setting.setClientCertificateFile(clientCertificateFileField.getText());
        setting.setClientKeyFile(clientKeyFileField.getText());
        setting.setClientKeyPassword(String.valueOf(clientKeyPasswordField.getPassword()));
        setting.setClientKeyPEM(clientKeyPemCheckBox.isSelected());
        return setting;
    }

    /**
     * Method generated by IntelliJ IDEA GUI Designer
     * >>> IMPORTANT!! <<<
     * DO NOT edit this method OR call it in your code!
     *
     * @noinspection ALL
     */
    private void $$$setupUI$$$() {
        rootPanel = new JPanel();
        rootPanel.setLayout(new FormLayout("fill:170px:noGrow,left:4dlu:noGrow,fill:d:grow", "center:d:noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow,top:4dlu:noGrow,center:max(d;4px):noGrow"));
        serverCertificateFileLabel = new JLabel();
        serverCertificateFileLabel.setText("CA certificate file");
        CellConstraints cc = new CellConstraints();
        rootPanel.add(serverCertificateFileLabel, cc.xy(1, 1));
        serverCertificateFileField = new JTextField();
        rootPanel.add(serverCertificateFileField, cc.xy(3, 1, CellConstraints.FILL, CellConstraints.DEFAULT));
        clientKeyPasswordLabel = new JLabel();
        clientKeyPasswordLabel.setText("Client key password");
        rootPanel.add(clientKeyPasswordLabel, cc.xy(1, 7));
        clientKeyPasswordField = new JPasswordField();
        rootPanel.add(clientKeyPasswordField, cc.xy(3, 7, CellConstraints.FILL, CellConstraints.DEFAULT));
        clientCertificateFileLabel = new JLabel();
        clientCertificateFileLabel.setText("Client certificate file");
        rootPanel.add(clientCertificateFileLabel, cc.xy(1, 3));
        clientKeyFileLabel = new JLabel();
        clientKeyFileLabel.setText("Client key file");
        rootPanel.add(clientKeyFileLabel, cc.xy(1, 5));
        clientCertificateFileField = new JTextField();
        rootPanel.add(clientCertificateFileField, cc.xy(3, 3, CellConstraints.FILL, CellConstraints.DEFAULT));
        clientKeyFileField = new JTextField();
        rootPanel.add(clientKeyFileField, cc.xy(3, 5, CellConstraints.FILL, CellConstraints.DEFAULT));
        clientKeyPemCheckBox = new JCheckBox();
        clientKeyPemCheckBox.setText("PEM key");
        rootPanel.add(clientKeyPemCheckBox, cc.xy(3, 9));
    }

    /**
     * @noinspection ALL
     */
    public JComponent $$$getRootComponent$$$() {
        return rootPanel;
    }

}
